package com.weixing.mall.provider.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.api.model.vo.ItemVo;
import com.weixing.mall.provider.api.model.vo.OrderGoodItvmVo;
import com.weixing.mall.provider.api.model.vo.OrderItemVo;
import com.weixing.mall.provider.api.service.ItemFeign;
import com.weixing.mall.provider.api.service.OrderServiceRpc;
import com.weixing.mall.provider.mapper.DefectiveProductMapper;
import com.weixing.mall.provider.model.domain.DefectiveProduct;
import com.weixing.mall.provider.model.dto.DefectiveProductDto;
import com.weixing.mall.provider.model.dto.DefectiveProductSaveDto;
import com.weixing.mall.provider.model.enums.DefectiveProductEunm;
import com.weixing.mall.provider.model.enums.DefectiveProductReasonEunm;
import com.weixing.mall.provider.model.query.DefectiveProductAppQuery;
import com.weixing.mall.provider.model.query.DefectiveProductQuery;
import com.weixing.mall.provider.model.vo.DefectiveProductAppVo;
import com.weixing.mall.provider.model.vo.DefectiveProductVo;
import com.weixing.mall.provider.model.vo.front.FrontDefectiveProductDto;
import com.weixing.mall.provider.service.IDefectiveProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 仓库模块-次品库商品表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-15
 */
@Service
public class DefectiveProductServiceImpl extends ServiceImpl<DefectiveProductMapper, DefectiveProduct> implements IDefectiveProductService {


    @Autowired
    private DefectiveProductMapper defectiveProductMapper;

    @Autowired
    private OrderServiceRpc orderServiceRpc;

    @Autowired
    ItemFeign  itemFeign;

    @Override
    public IPage<DefectiveProductVo> selectListOfPage(Page page, DefectiveProductQuery defectiveProductQuery) {
        IPage<DefectiveProductVo> defectiveProductVoIPage = defectiveProductMapper.selectListOfPage(page, defectiveProductQuery);
//        List<DefectiveProductVo> records = defectiveProductVoIPage.getRecords();
//        for (DefectiveProductVo record : records) {
//            record.setOrderType(orderServiceRpc.selectByAgentSn(record.getOrderSn()));
//        }
        System.out.println(defectiveProductVoIPage);
        return defectiveProductVoIPage;
    }

    @Override
    public void updated(DefectiveProductDto dp) {
        DefectiveProduct addDp = new DefectiveProduct();
        BeanUtils.copyProperties(dp,addDp);
        addDp.setProductStatus(dp.getProductStatus().getValue());
        addDp.setOutDepotTime(new Date());
        addDp.setRemark(dp.getRemark());
        updateById(addDp);
    }

    @Override
    public void create(DefectiveProductSaveDto dto, SysAuth auth) {
        DefectiveProduct defectiveProduct = new DefectiveProduct();
        BeanUtils.copyProperties(dto,defectiveProduct);
        defectiveProduct.setReason(dto.getReason().getValue());
        defectiveProduct.setCreatedTime(new Date());
        saveDefectiveProduct(dto.getProductSn(),dto.getOrderId(),auth,defectiveProduct);
    }

    private  void saveDefectiveProduct(String productSn,Long orderId,SysAuth auth,DefectiveProduct defectiveProduct){
        try {//如果RPC失败，则商品名称可为空
            ItemVo vo = itemFeign.findProductVoByProductSn(productSn).getData();
            defectiveProduct.setSpec(vo.getProductSkuDesp());
            defectiveProduct.setProductImg(vo.getProductImg());
            defectiveProduct.setProductSkuDesp(vo.getProductSkuDesp());
            defectiveProduct.setProductName(vo.getProductName());
        } catch (Exception e) {

        }
        OrderItemVo orderItemVo= orderServiceRpc.selectById(orderId).getData();

        defectiveProduct.setProductStatus(DefectiveProductEunm.TYPE_1.getValue());
        defectiveProduct.setCreatedTime(new Date());
        defectiveProduct.setOrderSn(orderItemVo.getOrderSn());
        defectiveProduct.setCostPrice(orderItemVo.getOrderPrice());
        defectiveProduct.setActName(orderItemVo.getActName());
        defectiveProduct.setMemberName(orderItemVo.getBuyer());
        defectiveProduct.setCustomerName(orderItemVo.getCustomerName());
        defectiveProduct.setOrderType(orderItemVo.getOrderType().toString());
        defectiveProduct.setAgentName(orderItemVo.getAgentName());
        defectiveProduct.setCreatedBy(auth.getUid());
        save(defectiveProduct);
    }
    @Override
    public void createFront(FrontDefectiveProductDto dto, SysAuth auth) {

        DefectiveProduct defectiveProduct = new DefectiveProduct();
        defectiveProduct.setRemark(dto.getRemark());
        defectiveProduct.setReason(dto.getReason().getValue());
        defectiveProduct.setOrderSn(dto.getOrderSn());
        defectiveProduct.setProductSn(dto.getProductSn());
        saveDefectiveProduct(dto.getProductSn(),dto.getOrderId(),auth,defectiveProduct);

        //变更售后表审核结果
       orderServiceRpc.updateAfterSaleStatus(dto.getAfterSaleId(),2,auth.getUid(),auth.getRealName());
    }
    @Override
    public boolean checkOrder(FrontDefectiveProductDto dto) {
        OrderItemVo orderItemVo= orderServiceRpc.selectById(dto.getOrderId()).getData();
        if(orderItemVo.getAfterSaleTag().intValue()==2&&dto.getReason().getValue().intValue()==DefectiveProductReasonEunm.TYPE_1.getValue().intValue()){
             if(dto.getImgurlList().size()>0&&dto.getImgurlList().size()<5){
                 return  true;
             }else{
                 return  false;
             }
        }
        return  true;
    }

    @Override
    public IPage<DefectiveProductAppVo> selectDefectiveProductList(Page page, DefectiveProductAppQuery defectiveProductAppQuery) {
        return defectiveProductMapper.selectDefectiveProductList(page,defectiveProductAppQuery);
    }

    @Override
    public DefectiveProductAppVo selectDefectiveProductById(long id) {
        return defectiveProductMapper.selectDefectiveProductById(id);
    }

    @Override
    public void createRpc(Long orderId, Integer type, Long uid,String remark) {
        DefectiveProduct defectiveProduct = new DefectiveProduct();
        defectiveProduct.setRemark(remark);
        defectiveProduct.setReason(type);
        defectiveProduct.setType(type.toString());
        OrderItemVo orderItemVo= orderServiceRpc.selectById(orderId).getData();
        defectiveProduct.setOrderSn(orderItemVo.getOrderSn());
        OrderGoodItvmVo  goodItvmVo=orderServiceRpc.getOrderGoodById(orderId);
        defectiveProduct.setProductSn(goodItvmVo.getGoodsSn());
        defectiveProduct.setAgentName(orderItemVo.getAgentName());
        defectiveProduct.setActName(orderItemVo.getActName());
        //TODO 这个地方不对，以后在进行修改为商品表中的成本价
        defectiveProduct.setCostPrice(orderItemVo.getOrderPrice());
        defectiveProduct.setCreatedBy(uid);
        defectiveProduct.setCustomerName(orderItemVo.getCustomerName());
        defectiveProduct.setMemberName(orderItemVo.getBuyer());
        defectiveProduct.setOrderType(orderItemVo.getOrderType().toString());
        defectiveProduct.setProductImg(goodItvmVo.getGoodsImg());
        defectiveProduct.setProductName(goodItvmVo.getGoodsName());
        defectiveProduct.setProductSkuDesp(goodItvmVo.getSkuDesp());
        defectiveProduct.setSpec(goodItvmVo.getSkuDesp());
        defectiveProduct.setOrderSn(orderItemVo.getOrderSn());
        defectiveProduct.setProductStatus(DefectiveProductEunm.TYPE_1.getValue());
        save(defectiveProduct);

    }

}
